package net.mcreator.makecustomitem.procedure;

import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.event.entity.living.LivingAttackEvent;
import net.minecraftforge.common.MinecraftForge;

import net.minecraft.world.World;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.math.BlockPos;
import net.minecraft.server.MinecraftServer;
import net.minecraft.potion.PotionEffect;
import net.minecraft.init.MobEffects;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.Entity;
import net.minecraft.command.ICommandSender;

import net.mcreator.makecustomitem.ElementsMakecustomitemMod;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.Executors;
import java.util.Map;

@ElementsMakecustomitemMod.ModElement.Tag
public class ProcedureDefenseUse extends ElementsMakecustomitemMod.ModElement {
	public ProcedureDefenseUse(ElementsMakecustomitemMod instance) {
		super(instance, 8);
	}

	public static void executeProcedure(Map<String, Object> dependencies) {
		if (dependencies.get("entity") == null) {
			System.err.println("Failed to load dependency entity for procedure DefenseUse!");
			return;
		}
		Entity entity = (Entity) dependencies.get("entity");
		entity.getEntityData().setDouble("MCIDefenseTemp", ((entity instanceof EntityLivingBase) ? ((EntityLivingBase) entity).getHealth() : -1));
		Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors()).schedule(() -> {
			if ((((entity instanceof EntityLivingBase) ? ((EntityLivingBase) entity).getHealth() : -1) > 0)) {
				entity.getEntityData().setDouble("MCIDefenseTemp2", ((entity.getEntityData().getDouble("MCIDefenseTemp"))
						- ((entity instanceof EntityLivingBase) ? ((EntityLivingBase) entity).getHealth() : -1)));
				if (((entity.getEntityData().getDouble(
						"Defense")) <= (((entity instanceof EntityLivingBase) ? ((EntityLivingBase) entity).getMaxHealth() : -1) * 0.25))) {
					if (((entity.getEntityData().getDouble("MCIDefenseTemp2")) <= (entity.getEntityData().getDouble("Defense")))) {
						if (entity instanceof EntityLivingBase)
							((EntityLivingBase) entity)
									.setHealth((float) (((entity instanceof EntityLivingBase) ? ((EntityLivingBase) entity).getHealth() : -1)
											+ (entity.getEntityData().getDouble("MCIDefenseTemp2"))));
					} else {
						if (entity instanceof EntityLivingBase)
							((EntityLivingBase) entity)
									.setHealth((float) (((entity instanceof EntityLivingBase) ? ((EntityLivingBase) entity).getHealth() : -1)
											+ (entity.getEntityData().getDouble("Defense"))));
					}
				} else {
					{
						Entity _ent = entity;
						if (!_ent.world.isRemote && _ent.world.getMinecraftServer() != null) {
							_ent.world.getMinecraftServer().getCommandManager().executeCommand(new ICommandSender() {
								@Override
								public String getName() {
									return "";
								}

								@Override
								public boolean canUseCommand(int permission, String command) {
									return true;
								}

								@Override
								public World getEntityWorld() {
									return _ent.world;
								}

								@Override
								public MinecraftServer getServer() {
									return _ent.world.getMinecraftServer();
								}

								@Override
								public boolean sendCommandFeedback() {
									return false;
								}

								@Override
								public BlockPos getPosition() {
									return _ent.getPosition();
								}

								@Override
								public Vec3d getPositionVector() {
									return new Vec3d(_ent.posX, _ent.posY, _ent.posZ);
								}

								@Override
								public Entity getCommandSenderEntity() {
									return _ent;
								}
							}, "tellraw @s {\"text\":\"\u00A7c\u5F53\u524D\u751F\u547D\u5F3A\u5EA6\u65E0\u6CD5\u652F\u6301\u8FC7\u5927\u7684\u9632\u5FA1\u529B\"}");
						}
					}
					if (entity instanceof EntityLivingBase)
						((EntityLivingBase) entity).addPotionEffect(new PotionEffect(MobEffects.WEAKNESS, (int) 60, (int) 1, (false), (true)));
					if (entity instanceof EntityLivingBase)
						((EntityLivingBase) entity).addPotionEffect(new PotionEffect(MobEffects.SLOWNESS, (int) 60, (int) 1, (false), (true)));
					if (entity instanceof EntityLivingBase)
						((EntityLivingBase) entity)
								.setHealth((float) (((entity instanceof EntityLivingBase) ? ((EntityLivingBase) entity).getHealth() : -1)
										+ (((entity instanceof EntityLivingBase) ? ((EntityLivingBase) entity).getMaxHealth() : -1) * 0.25)));
				}
			}
		}, 50, TimeUnit.MILLISECONDS);
	}

	@SubscribeEvent
	public void onEntityAttacked(LivingAttackEvent event) {
		if (event != null && event.getEntity() != null) {
			Entity entity = event.getEntity();
			int i = (int) entity.posX;
			int j = (int) entity.posY;
			int k = (int) entity.posZ;
			World world = entity.world;
			java.util.HashMap<String, Object> dependencies = new java.util.HashMap<>();
			dependencies.put("x", i);
			dependencies.put("y", j);
			dependencies.put("z", k);
			dependencies.put("world", world);
			dependencies.put("entity", entity);
			dependencies.put("event", event);
			dependencies.put("sourceentity", event.getSource().getImmediateSource());
			this.executeProcedure(dependencies);
		}
	}

	@Override
	public void preInit(FMLPreInitializationEvent event) {
		MinecraftForge.EVENT_BUS.register(this);
	}
}
